﻿using System.Web;
using WikiMaml.Renderers;
using WikiPlex;
using Xunit;
using Xunit.Extensions;

namespace WikiMaml.Facts.Renderers
{
    public class TextFormattingRendererFacts
    {
        public class CanExpand
        {
            [Theory]            
            [InlineData(ScopeName.BoldBegin)]
            [InlineData(ScopeName.BoldEnd)]
            [InlineData(ScopeName.ItalicsBegin)]
            [InlineData(ScopeName.ItalicsEnd)]
            [InlineData(ScopeName.UnderlineBegin)]
            [InlineData(ScopeName.UnderlineEnd)]
            [InlineData(ScopeName.Remove)]
            [InlineData(ScopeName.SuperscriptBegin)]
            [InlineData(ScopeName.SuperscriptEnd)]
            [InlineData(ScopeName.SubscriptBegin)]
            [InlineData(ScopeName.SubscriptEnd)]
            [InlineData(ScopeName.EscapedMarkup)]
            public void Should_be_able_to_resolve_scope_name(string scopeName)
            {
                var renderer = new TextFormattingRenderer();

                bool result = renderer.CanExpand(scopeName);

                Assert.True(result);
            }
        }

        public class Expand
        {
            [Theory]
            [InlineData(ScopeName.BoldBegin, "legacyBold")]
            [InlineData(ScopeName.BoldEnd, "/legacyBold")]
            [InlineData(ScopeName.ItalicsBegin, "legacyItalic")]
            [InlineData(ScopeName.ItalicsEnd, "/legacyItalic")]
            [InlineData(ScopeName.UnderlineBegin, "legacyUnderline")]
            [InlineData(ScopeName.UnderlineEnd, "/legacyUnderline")]
            [InlineData(ScopeName.SuperscriptBegin, "superscript")]
            [InlineData(ScopeName.SuperscriptEnd, "/superscript")]
            [InlineData(ScopeName.SubscriptBegin, "subscript")]
            [InlineData(ScopeName.SubscriptEnd, "/subscript")]
            public void Should_resolve_the_scope_correctly_for_tags(string scopeName, string tagName)
            {
                var renderer = new TextFormattingRenderer();

                string result = renderer.Expand(scopeName, "in", x => x, x => x);

                Assert.Equal(string.Format("<{0}>", tagName), result);
            }

            [Fact]
            public void Should_resolve_the_remove_scope_correctly()
            {
                var renderer = new TextFormattingRenderer();

                string result = renderer.Expand(ScopeName.Remove, "in", x => x, x => x);

                Assert.Empty(result);
            }

            [Fact]
            public void Should_resolve_the_escaped_markup_scope_correctly()
            {
                var renderer = new TextFormattingRenderer();

                string result = renderer.Expand(ScopeName.EscapedMarkup, "this is content", x => x, x => x);

                Assert.Equal("this is content", result);
            }

            [Fact]
            public void Should_resolve_the_escaped_markup_scope_correctly_and_html_encode_it()
            {
                var renderer = new TextFormattingRenderer();

                string result = renderer.Expand(ScopeName.EscapedMarkup, "this is &content", HttpUtility.HtmlEncode, x => x);

                Assert.Equal("this is &amp;content", result);
            }

            [Fact]
            public void Should_return_content_for_invalid_scope_name()
            {
                var renderer = new TextFormattingRenderer();

                string result = renderer.Expand("foo", "in", x => x, x => x);

                Assert.Equal("in", result);
            }

        }
    }
}
